/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.swta.freezay.database.controller;

import de.swta.freezay.database.controller.exceptions.NonexistentEntityException;
import de.swta.freezay.database.controller.exceptions.PreexistingEntityException;
import de.swta.freezay.database.dbEntities.ItemPackage;
import de.swta.freezay.database.dbEntities.ItemPackagePK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import de.swta.freezay.database.dbEntities.Item;
import de.swta.freezay.database.dbEntities.Package;

/**
 *
 * @author Christian
 */
public class ItemPackageJpaController {

    public ItemPackageJpaController() {
        emf = Persistence.createEntityManagerFactory("FreezayPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ItemPackage itemPackage) throws PreexistingEntityException, Exception {
        if (itemPackage.getItemPackagePK() == null) {
            itemPackage.setItemPackagePK(new ItemPackagePK());
        }
        itemPackage.getItemPackagePK().setPackageId(itemPackage.getPackage1().getId());
        itemPackage.getItemPackagePK().setItemId(itemPackage.getItem().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Item item = itemPackage.getItem();
            if (item != null) {
                item = em.getReference(item.getClass(), item.getId());
                itemPackage.setItem(item);
            }
            Package package1 = itemPackage.getPackage1();
            if (package1 != null) {
                package1 = em.getReference(package1.getClass(), package1.getId());
                itemPackage.setPackage1(package1);
            }
            em.persist(itemPackage);
            if (item != null) {
                item.getItemPackageCollection().add(itemPackage);
                item = em.merge(item);
            }
            if (package1 != null) {
                package1.getItemPackageCollection().add(itemPackage);
                package1 = em.merge(package1);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findItemPackage(itemPackage.getItemPackagePK()) != null) {
                throw new PreexistingEntityException("ItemPackage " + itemPackage + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ItemPackage itemPackage) throws NonexistentEntityException, Exception {
        itemPackage.getItemPackagePK().setPackageId(itemPackage.getPackage1().getId());
        itemPackage.getItemPackagePK().setItemId(itemPackage.getItem().getId());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ItemPackage persistentItemPackage = em.find(ItemPackage.class, itemPackage.getItemPackagePK());
            Item itemOld = persistentItemPackage.getItem();
            Item itemNew = itemPackage.getItem();
            Package package1Old = persistentItemPackage.getPackage1();
            Package package1New = itemPackage.getPackage1();
            if (itemNew != null) {
                itemNew = em.getReference(itemNew.getClass(), itemNew.getId());
                itemPackage.setItem(itemNew);
            }
            if (package1New != null) {
                package1New = em.getReference(package1New.getClass(), package1New.getId());
                itemPackage.setPackage1(package1New);
            }
            itemPackage = em.merge(itemPackage);
            if (itemOld != null && !itemOld.equals(itemNew)) {
                itemOld.getItemPackageCollection().remove(itemPackage);
                itemOld = em.merge(itemOld);
            }
            if (itemNew != null && !itemNew.equals(itemOld)) {
                itemNew.getItemPackageCollection().add(itemPackage);
                itemNew = em.merge(itemNew);
            }
            if (package1Old != null && !package1Old.equals(package1New)) {
                package1Old.getItemPackageCollection().remove(itemPackage);
                package1Old = em.merge(package1Old);
            }
            if (package1New != null && !package1New.equals(package1Old)) {
                package1New.getItemPackageCollection().add(itemPackage);
                package1New = em.merge(package1New);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ItemPackagePK id = itemPackage.getItemPackagePK();
                if (findItemPackage(id) == null) {
                    throw new NonexistentEntityException("The itemPackage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ItemPackagePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ItemPackage itemPackage;
            try {
                itemPackage = em.getReference(ItemPackage.class, id);
                itemPackage.getItemPackagePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The itemPackage with id " + id + " no longer exists.", enfe);
            }
            Item item = itemPackage.getItem();
            if (item != null) {
                item.getItemPackageCollection().remove(itemPackage);
                item = em.merge(item);
            }
            Package package1 = itemPackage.getPackage1();
            if (package1 != null) {
                package1.getItemPackageCollection().remove(itemPackage);
                package1 = em.merge(package1);
            }
            em.remove(itemPackage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public boolean isUsed(ItemPackagePK id) {
        boolean isUsed = false;

        // noch mal drüber schauen!
        ItemPackage itemPackageToTest = this.findItemPackage(id);

        if (itemPackageToTest != null) {
            isUsed = (itemPackageToTest.getItemPackagePK() != null
                    || itemPackageToTest.getPackage1() != null);
        }

        return isUsed;
    }

    public List<ItemPackage> findItemPackageEntities() {
        return findItemPackageEntities(true, -1, -1);
    }

    public List<ItemPackage> findItemPackageEntities(int maxResults, int firstResult) {
        return findItemPackageEntities(false, maxResults, firstResult);
    }

    private List<ItemPackage> findItemPackageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from ItemPackage as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ItemPackage findItemPackage(ItemPackagePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ItemPackage.class, id);
        } finally {
            em.close();
        }
    }

    public int getItemPackageCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from ItemPackage as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
